package cn.hema.controller;

import cn.hema.Client.BorrowerServiceClient;
import cn.hema.model.Borrower;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@AllArgsConstructor
@RestController
@Slf4j
public class BorrowerServiceClientController {

    @Autowired
    private final BorrowerServiceClient borrowerServiceClient;

    /**
     * 根据借阅人 id 查询借阅人
     */
    @GetMapping("/borrowers/{id}")
    public Borrower getBorrower(@PathVariable Long id) {
        Borrower borrower = borrowerServiceClient.getBorrower(id);
        log.info("-------------In client getBorrower---------------");
        return borrower;
    }

    /**
     * 查询所有借阅人
     */
    @GetMapping("/borrowers")
    public List<Borrower> getAllBorrowers() {
        List<Borrower> productList = borrowerServiceClient.getAllBorrowers();
        log.info("--------------In client getAllBorrowers--------------");
        return productList;
    }

    /**
     * 新增一个借阅人
     */
    @PostMapping("/borrowers")
    public void createBorrower(@RequestBody Borrower borrower) {
        log.info("-------------In client createBorrower-------------");
        borrowerServiceClient.createBorrower(borrower);
    }

    /**
     * 更新一个借阅人信息
     */
    @PutMapping("/borrowers/{id}")
    public void updateBorrower(@PathVariable Long id, @RequestBody Borrower borrower) {
        log.info("-------------In client updateBorrower-------------");
        borrowerServiceClient.updateBorrower(id, borrower);
    }

    /**
     * 根据id删除借阅人信息
     */
    @DeleteMapping("/borrowers/{id}")
    public void deleteBorrower(@PathVariable Long id) {
        log.info("-------------In client deleteBorrower-------------");
        borrowerServiceClient.deleteBorrower(id);
    }
}
